package com.ruoyi.activity.service.impl;

import java.util.List;

import cn.hutool.json.JSONUtil;
import com.ruoyi.activity.model.domain.Workinformation;
import com.ruoyi.activity.mapper.WorkMapper;
import com.ruoyi.activity.mapper.WorkinformationMapper;
import com.ruoyi.activity.model.domain.Work;
import com.ruoyi.client.model.dto.ApiWorkRequest;
import com.ruoyi.client.vo.ApiExpectWorkVO;
import com.ruoyi.activity.service.IWorkService;
import com.ruoyi.client.vo.UserLoginVO;
import com.ruoyi.common.utils.file.FileUploadUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * 作品信息Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-28
 */
@Service
public class WorkServiceImpl implements IWorkService {
    @Autowired
    private WorkMapper workMapper;

    @Autowired
    private WorkinformationMapper workinformationMapper;

    /**
     * 查询作品信息
     *
     * @param id 作品信息主键
     * @return 作品信息
     */
    @Override
    public Work selectWorkById(Long id) {
        return workMapper.selectWorkById(id);
    }

    /**
     * 查询作品信息列表
     *
     * @param work 作品信息
     * @return 作品信息
     */
    @Override
    public List<Work> selectWorkList(Work work) {
        return workMapper.selectWorkList(work);
    }

    /**
     * 新增作品信息
     *
     * @param work 作品信息
     * @return 结果
     */
    @Override
    public int insertWork(Work work) {
        return workMapper.insertWork(work);
    }

    /**
     * 修改作品信息
     *
     * @param work 作品信息
     * @return 结果
     */
    @Override
    public int updateWork(Work work) {
        return workMapper.updateWork(work);
    }

    /**
     * 批量删除作品信息
     *
     * @param ids 需要删除的作品信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkByIds(Long[] ids) {
        return workMapper.deleteWorkByIds(ids);
    }

    /**
     * 删除作品信息信息
     *
     * @param id 作品信息主键
     * @return 结果
     */
    @Override
    public int deleteWorkById(Long id) {
        return workMapper.deleteWorkById(id);
    }

    /**
     * 报名比赛
     *
     * @param apiWorkRequest
     * @param loginUserVO
     * @return
     */
    @Override
    public boolean joinWork(ApiWorkRequest apiWorkRequest, UserLoginVO loginUserVO) {
        Work work = new Work();
        BeanUtils.copyProperties(apiWorkRequest, work);
        List<String> participantNameList = apiWorkRequest.getParticipantNameList();
        String userNameJsonStr = JSONUtil.toJsonStr(participantNameList);
        work.setWorkName(userNameJsonStr);
        Long userId = loginUserVO.getId();
        work.setUserId(userId);
        List<MultipartFile> multipartFileList = apiWorkRequest.getMultipartFileList();
        if (multipartFileList == null) {
            return false;
        }
        try {
            for (MultipartFile multipartFile : multipartFileList) {
                FileUploadUtils.upload(multipartFile);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //TODO 这里是可能需要根据需求进行改变
        work.setCourseStandard(FileUploadUtils.getDefaultBaseDir());
        work.setClassroomRecording(FileUploadUtils.getDefaultBaseDir());
        work.setLessonPlan(FileUploadUtils.getDefaultBaseDir());
        work.setTeachingPracticeReport(FileUploadUtils.getDefaultBaseDir());
        work.setMajorTalentDevelopmentPlan(FileUploadUtils.getDefaultBaseDir());
        int saveWork = workMapper.insertWork(work);
        if (saveWork <= 0) {
            return false;
        }
        Workinformation workinformation = new Workinformation();
        workinformation.setWorkId(work.getId());
        workinformation.setAvatar(FileUploadUtils.getDefaultBaseDir());
        workinformation.setInformationDisclosureDocument(FileUploadUtils.getDefaultBaseDir());
        workinformationMapper.insertWorkinformation(workinformation);
        return true;
    }

    @Override
    public ApiExpectWorkVO getApiExceptWorkVo(Work work) {
        ApiExpectWorkVO apiExpectWorkVo = new ApiExpectWorkVO();
        BeanUtils.copyProperties(work, apiExpectWorkVo);
        return apiExpectWorkVo;
    }
}
